﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DonationTracker
{
    public partial class Events : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (User.Identity.IsAuthenticated)
            {
                if (User.IsInRole("Administrator"))
                {
                    GridView1.AutoGenerateEditButton = true;
                    GridView1.AutoGenerateDeleteButton = true;
                }
                if (!IsPostBack) GridView1.ShowFooter = false;
            }
        }

        protected void btnAddEvent_Click(object sender, EventArgs e)
        {
            GridView1.ShowFooter = true;
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Insert" && Page.IsValid)
            {
                TextBox txtNewName =
                    (TextBox)GridView1.FooterRow.FindControl("txtNewName");
                TextBox txtNewDesc =
                    (TextBox)GridView1.FooterRow.FindControl("txtNewDesc");
                DropDownList ddlNewCharityID =
                    (DropDownList)GridView1.FooterRow.FindControl("ddlNewCharityID");

                string name, desc, id;
                name = txtNewName.Text.Trim();
                desc = txtNewDesc.Text.Trim();
                id = ddlNewCharityID.SelectedValue;
                if (String.IsNullOrWhiteSpace(desc)) desc = null;

                donationTracker_dataSource.InsertParameters["name"] =
                    new Parameter("name", System.Data.DbType.String, name);
                donationTracker_dataSource.InsertParameters["description"] =
                    new Parameter("description", System.Data.DbType.String, desc);
                donationTracker_dataSource.InsertParameters["charityID"] =
                    new Parameter("charityID", System.Data.DbType.Int32, id);

                donationTracker_dataSource.Insert();
                GridView1.ShowFooter = false;
            }
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtEditName =
                    (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditName");
            TextBox txtEditDesc =
                (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditDesc");
            DropDownList ddlEditCharityID =
                (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlEditCharityID");

            string name, desc, id;
            name = txtEditName.Text.Trim();
            desc = txtEditDesc.Text.Trim();
            id = ddlEditCharityID.SelectedValue;
            if (String.IsNullOrWhiteSpace(desc))
                desc = null;

            donationTracker_dataSource.UpdateParameters["name"] =
                new Parameter("name", System.Data.DbType.String, name);
            donationTracker_dataSource.UpdateParameters["description"] =
                new Parameter("description", System.Data.DbType.String, desc);
            donationTracker_dataSource.UpdateParameters["charityID"] =
                new Parameter("charityID", System.Data.DbType.Int32, id);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            GridView1.ShowFooter = false;
        }
    }
}